package com.rms.hr.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rms.hr.model.User;
import com.rms.hr.service.UserService;

/**
 * @author lgw0822
 *
 *      2013-07-06
 */
@Controller
@RequestMapping("/userController")
public class UserController {
	
	 /** The UserController logger. */
	 private static final Logger log = Logger.getLogger(UserController.class);
    private UserService userService;
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	/**
	 * 查询所有的用户
	 * @return
	 */
	@RequestMapping("/getUserList")
	public ModelAndView getUserList(){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("userList",this.userService.getAllUser());
		modelAndView.setViewName("page/hr/user/userList");
//		modelAndView.setViewName("redirect:/userList.jsp");

		return modelAndView;
	}
	/**
	 * 添加用户前，为每个用户分配唯一标识
	 * @return
	 */
	@RequestMapping("/inputAddUser")
	public ModelAndView inputAddUser(){
		ModelAndView modelAndView =  new ModelAndView("page/hr/user/userAdd");
		modelAndView.addObject("code",System.currentTimeMillis());
		return modelAndView;
	}
	
	/**
	 * 添加用户前
	 * @return
	 */
	@RequestMapping("/addUser")
	public ModelAndView addUser(User user){
		if(user!=null){
			int id = this.userService.creatUser(user);
			log.info("【第 "+id+" 位用户添加成功】");
		}
		ModelAndView modelAndView =  new ModelAndView();
		//为保存不重复提交，使用redirect
		modelAndView.setViewName("redirect:/userController/getUserList.do");
		return modelAndView;
//		return getUserList();
	}
	
	/**
	 * 更新用户前，查出用户我所有信息
	 * @return
	 */
	@RequestMapping("/inputUpdateUser")
	public ModelAndView inputUpdateUser(Integer id){
		ModelAndView mav = new ModelAndView("page/hr/user/userUpdate");
		mav.addObject("user",this.userService.selectByPrimaryKey(id));
		return mav;
	}
	/**
	 * 更新用户
	 * @return
	 * @ModelAttribute("user")
	 */
	@RequestMapping("/updateUser")
	public ModelAndView updateUser(User user){
		if(user!=null){
			int id = this.userService.updateUser(user);
			log.info("【第 "+id+" 位用户更新成功】");
		}
		ModelAndView modelAndView =  new ModelAndView();
		//为保存不重复提交，使用redirect
		modelAndView.setViewName("redirect:/userController/getUserList.do");
		return modelAndView;
	}
	
	/**
	 * 删除用户
	 * @return
	 */
	@RequestMapping("/deleteUserById")
	public ModelAndView deleteUserById(Integer id){
		
		this.userService.deleteUserById(id);
		log.info("【第 "+id+" 位用户删除成功】");
		ModelAndView modelAndView =  new ModelAndView();
		//为保存不重复提交，使用redirect
		modelAndView.setViewName("redirect:/userController/getUserList.do");
		return modelAndView;
	}
	
}
